package com.colorfruit.admin.control.controller;

import com.colorfruit.admin.business.service.LoginService;
import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.framework.model.pojo.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    LoginService loginService;

    @GetMapping("/wxLogin")
    public BaseResult wxLogin(String code,
                              String nickName,
                              String userImgUrl,
                              Byte userSex){
        try {
            log.info("# [IN] [controller] wxLogin param: code = {}, nickName = {}, userImgUrl = {}, userSex = {}",code, nickName, userImgUrl, userSex);
            BaseResult baseResult = loginService.wxLogin(code, nickName, userImgUrl, userSex);
            log.info("# [OUT] [controller] wxLogin SUCCESS");
            return baseResult;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    @GetMapping("/testRedis")
    public BaseResult testRedis(){
        try {
            return loginService.testRedis();
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            return new BaseResult(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

}
